class TFTab_LoadoutSettings extends Settings_Tabs;

var automated GUISectionBackground  i_BG1, i_BG2;           //Background
var automated moComboBox            co_Loadoutselection;    //Loadout selection combo box
var int                             currentindex, oldindex; //Index we are currently editing, previous index
var automated moEditBox             ed_LoadoutName;         //Loadout name field
var automated moComboBox            co_LoadoutPick[9];      //Array for all 9 items in the loadout
var automated GUIButton             b_savebutton;           //Button for saving
var localized array<string>         itemlist;               //The items the user can choose from
var localized array<string>         namelist;               //List of names to display in place of the class names of items
var TFLoadouts                      Loadouts;
var PlayerController                PC;

//Draw initial GUI components
function InitComponent(GUIController MyController, GUIComponent MyOwner)
{
    local int i, limit;

	Super.Initcomponent(MyController, MyOwner);

    PC = PlayerOwner();
    Loadouts = PlayerOwner().Spawn(class'TFLoadouts', PC,,vect(0,0,0),rot(0,0,0));

	i=0;
	limit=9;

    //Draw fields
    i_BG2.Managecomponent(co_Loadoutselection);
	i_BG2.Managecomponent(ed_LoadoutName);
	i_BG1.ManageComponent(b_savebutton);

    //Draw array fields
    while(i<limit)
    {
        co_Loadoutselection.AddItem(Loadouts.GetLoadoutName(i));
        i_BG1.ManageComponent(co_LoadoutPick[i]);
        i++;
    }

    i=0;
    limit=namelist.length;

    //Fill the item boxes with choices
    while(i<limit)
    {
        co_LoadoutPick[0].AddItem(namelist[i]);
        co_LoadoutPick[1].AddItem(namelist[i]);
        co_LoadoutPick[2].AddItem(namelist[i]);
        co_LoadoutPick[3].AddItem(namelist[i]);
        co_LoadoutPick[4].AddItem(namelist[i]);
        co_LoadoutPick[5].AddItem(namelist[i]);
        co_LoadoutPick[6].AddItem(namelist[i]);
        co_LoadoutPick[7].AddItem(namelist[i]);
        co_LoadoutPick[8].AddItem(namelist[i]);
        i++;
    }

    //Fill in the fields
    Loadouts.Destroy();
    UpdateInfo();
}

//Initiates the loadout set change upon changing the combo box option
function InternalOnChange(GUIComponent Sender)
{
    //if the sender was the combobox then update
	if(Sender == co_Loadoutselection)
	{
	   UpdateInfo();
	}
}

//Saves the current loadout when the save button is pressed
function bool SaveOnClick(GUIComponent Sender)
{
    if(Sender == b_savebutton)
	{
        SaveSettings();
    }
    return true;
}

//Loads the current loadout info to the fields
function UpdateInfo()
{
	local int i, itemi;

    Loadouts = PC.Spawn(class'TFLoadouts', PC,,vect(0,0,0),rot(0,0,0));
    oldindex = currentindex;

    if(co_Loadoutselection.GetIndex() < 0 || co_Loadoutselection.GetIndex() > 8)
        currentindex = 0;
    else
        currentindex = co_Loadoutselection.GetIndex();

    //update the name field to show the name of the current loadout
	ed_LoadoutName.SetText(Loadouts.GetLoadoutName(currentindex));

    //upadte the item fields with the current loadout selection
	for(i=0;i<9;i++)
	{
	   for(itemi=0;itemi<itemlist.length;itemi++)
	   {
	       if(Loadouts.GetLoadoutItem(currentindex, i) == itemlist[itemi])
	       {
	           co_LoadoutPick[i].Setindex(itemi);
	           break;
	       }
	   }
	}

    Loadouts.Destroy();
}

//Saves the current loadout
function SaveSettings()
{
    local bool bSave;
	local int i, itemnum;

	Super.SaveSettings();

	Loadouts = PC.Spawn(class'TFLoadouts', PC,,vect(0,0,0),rot(0,0,0));

    //If the name of the loadout was changed
    if(ed_LoadoutName.GetText() != Loadouts.GetLoadoutName(currentindex))
	{
	   //Write the new name to the ini
	   Loadouts.SetLoadoutName(currentindex, ed_LoadoutName.GetText());

        //Replace the name in the selection box as well
        co_Loadoutselection.MyComboBox.List.replace(currentindex,Loadouts.GetLoadoutName(currentindex));

	   bSave = true;
	}

    //Write all items to the ini
    for(i=0;i<9;i++)
    {
        //Find out what item we selected
        itemnum=co_LoadoutPick[i].GetIndex();
        if(Loadouts.GetLoadoutItem(currentindex, i) != itemlist[itemnum])
        {
            //Write the class equivilant
            Loadouts.SetLoadoutItem(currentindex, i, itemlist[itemnum]);
            bSave = true;
        }
    }

    //Save the ini
    if(bSave)
	   SaveConfig();

	Loadouts.Destroy();
}

defaultproperties
{
    itemlist[0]="none"
    namelist[0]="None"
    itemlist[1]="Taskforces.TFAssaultrifle"
    namelist[1]="Assault Rifle"
    itemlist[2]="Taskforces.TFThermoBlaster"
    namelist[2]="Thermo Blasters"
    itemlist[3]="Taskforces.TFMinigun"
    namelist[3]="Minigun"
    itemlist[4]="Taskforces.TFFlakCannon"
    namelist[4]="Flak Cannon"
    itemlist[5]="Taskforces.TFRocketLauncher"
    namelist[5]="Rocket Launcher"
    itemlist[6]="Taskforces.TFMassDriver"
    namelist[6]="Mass Driver"
    itemlist[7]="Taskforces.TFDisruptor"
    namelist[7]="Disruptor"
    itemlist[8]="Taskforces.TFThermoProj"
    namelist[8]="Thermo Projector"
    itemlist[9]="Taskforces.TFProxyMineThrower"
    namelist[9]="Proxy Mines"
    itemlist[10]="Taskforces.TFClaw"
    namelist[10]="Trip Mines"
    itemlist[11]="Taskforces.TFGrenadeLauncher"
    namelist[11]="Grenade Launcher"
    itemlist[12]="Taskforces.TFMineLayer"
    namelist[12]="Spider Mines"
    itemlist[13]="Taskforces.TFTPainter"
    namelist[13]="Target Painter"
    itemlist[14]="Taskforces.TFDOTBomb"
    namelist[14]="DOT Bomb"
    itemlist[15]="Taskforces.TFIonPainter"
    namelist[15]="Ion Painter"
    itemlist[16]="Taskforces.TFFusionGatling"
    namelist[16]="Fusion Gatling"
    itemlist[17]="Taskforces.TFSMiniCannon"
    namelist[17]="Siege Mini-Cannon"
    itemlist[18]="Taskforces.TFTorsoBomb"
    namelist[18]="Torso Bomb"
    itemlist[19]="Taskforces.TFDepthRadar"
    namelist[19]="Depth Radar"
    itemlist[20]="Taskforces.TFLifeScanner"
    namelist[20]="Lifesign Scanner"
    itemlist[21]="Taskforces.TFBanner"
    namelist[21]="War Banner"
    itemlist[22]="Taskforces.TFReplicator"
    namelist[22]="Replicator"
    itemlist[23]="Taskforces.TFShieldBelt"
    namelist[23]="Shield Belt"
    itemlist[24]="Taskforces.TFThrustBoots"
    namelist[24]="Thrust Boots"
    itemlist[25]="Taskforces.TFShroud"
    namelist[25]="Shroud"
    itemlist[26]="Taskforces.TFMDGun"
    namelist[26]="Medic"
    itemlist[27]="Taskforces.TFMMGun"
    namelist[27]="Marksman"
    itemlist[28]="Taskforces.TFCMGun"
    namelist[28]="Commando"
    itemlist[29]="Taskforces.TFGunnerClass"
    namelist[29]="Heavy Gunner"
    itemlist[30]="Taskforces.TFParaTClass"
    namelist[30]="Bomber"
    itemlist[31]="Taskforces.TFRedeemer"
    namelist[31]="Shield Cracker"
    itemlist[32]="Taskforces.TFTGrenade"
    namelist[32]="T-Grenade"
    itemlist[33]="Taskforces.TFMIC"
    namelist[33]="M.I.C."
    itemlist[34]="Taskforces.TFWBGun"
    namelist[34]="Warp Blade"
    itemlist[35]="Taskforces.gDestroyer"
    namelist[35]="Multon Destroyer"
    itemlist[36]="Taskforces.TFCNGun"
    namelist[36]="Siege Cannon"
    itemlist[37]="Taskforces.TFWLGun"
    namelist[37]="Warhead Launcher"
    itemlist[38]="Taskforces.TFNKGun"
    namelist[38]="Nuke Mortar"

    Begin Object Class=GUISectionBackground Name=Background
        bFillClient=True
        Caption="Loadout Items"
        LeftPadding=0.020000
        RightPadding=0.020000
        TopPadding=0.020000
        BottomPadding=0.020000
        WinTop=0.15
        WinLeft=0.004063
        WinWidth=0.992000
        WinHeight=0.840000
        OnPreDraw=Background.InternalPreDraw
    End Object
    i_BG1=GUISectionBackground'Taskforces.TFTab_LoadoutSettings.Background'

         Begin Object Class=GUISectionBackground Name=Background2
         bFillClient=True
         Caption="Loadout"
         NumColumns=2
         MaxPerColumn=1
         LeftPadding=0.020000
         RightPadding=0.020000
         TopPadding=0.020000
         BottomPadding=0.020000
         WinTop=0.017393
         WinLeft=0.004063
         WinWidth=0.992000
         WinHeight=0.15
         OnPreDraw=Background.InternalPreDraw
     End Object
     i_BG2=GUISectionBackground'Taskforces.TFTab_LoadoutSettings.Background2'

    Begin Object Class=moComboBox Name=Loadoutslist
        bReadOnly=True
        ComponentJustification=TXTA_Left
        CaptionWidth=0.300000
        Caption="Loadout To Edit:"
        OnCreateComponent=Loadoutslist.InternalOnCreateComponent
        IniOption="@Internal"
        IniDefault="Loadout 1"
        Hint="Changes the loadout you are currently editing."
        WinTop=0.150261
        WinLeft=0.301757
        WinWidth=0.374219
        ComponentWidth=0.500000
        TabOrder=0
        bVerticalLayout=false
        OnChange=InternalOnChange
    End Object
    co_Loadoutselection=moComboBox'Taskforces.TFTab_LoadoutSettings.Loadoutslist'

    Begin Object class=moEditBox name=LoadoutName0
        ComponentJustification=TXTA_Right
        Caption="Loadout Name"
        OnCreateComponent=LoadoutName0.InternalOnCreateComponent
        IniOption="@INTERNAL"
        IniDefault="Unassigned"
        Hint="Changes the name of this loadout."
        WinTop=0.150261
        WinLeft=0.301757
        WinWidth=0.374219
        ComponentWidth=0.500000
        TabOrder=1
    End Object
    ed_LoadoutName[0]=moEditBox'Taskforces.TFTab_LoadoutSettings.LoadoutName0'

    Begin Object Class=moComboBox Name=LoadoutPick0
         bReadOnly=True
         ComponentJustification=TXTA_Left
         CaptionWidth=0.700000
         Caption="Loadout Item"
         OnCreateComponent=LoadoutPick0.InternalOnCreateComponent
         IniOption="@Internal"
         IniDefault="None"
         Hint="Changes the item you will get when you purchase this loadout."
         WinTop=0.150261
         WinLeft=0.301757
         WinWidth=0.374219
         TabOrder=2
    End Object
    co_LoadoutPick[0]=moComboBox'Taskforces.TFTab_LoadoutSettings.LoadoutPick0'

    Begin Object Class=moComboBox Name=LoadoutPick1
         bReadOnly=True
         ComponentJustification=TXTA_Left
         CaptionWidth=0.700000
         Caption="Loadout Item"
         OnCreateComponent=LoadoutPick1.InternalOnCreateComponent
         IniOption="@Internal"
         IniDefault="None"
         Hint="Changes the item you will get when you purchase this loadout."
         WinTop=0.150261
         WinLeft=0.301757
         WinWidth=0.374219
         TabOrder=2
    End Object
    co_LoadoutPick[1]=moComboBox'Taskforces.TFTab_LoadoutSettings.LoadoutPick1'

    Begin Object Class=moComboBox Name=LoadoutPick2
         bReadOnly=True
         ComponentJustification=TXTA_Left
         CaptionWidth=0.700000
         Caption="Loadout Item"
         OnCreateComponent=LoadoutPick2.InternalOnCreateComponent
         IniOption="@Internal"
         IniDefault="None"
         Hint="Changes the item you will get when you purchase this loadout."
         WinTop=0.150261
         WinLeft=0.301757
         WinWidth=0.374219
         TabOrder=2
    End Object
    co_LoadoutPick[2]=moComboBox'Taskforces.TFTab_LoadoutSettings.LoadoutPick2'

    Begin Object Class=moComboBox Name=LoadoutPick3
         bReadOnly=True
         ComponentJustification=TXTA_Left
         CaptionWidth=0.700000
         Caption="Loadout Item"
         OnCreateComponent=LoadoutPick3.InternalOnCreateComponent
         IniOption="@Internal"
         IniDefault="None"
         Hint="Changes the item you will get when you purchase this loadout."
         WinTop=0.150261
         WinLeft=0.301757
         WinWidth=0.374219
         TabOrder=2
    End Object
    co_LoadoutPick[3]=moComboBox'Taskforces.TFTab_LoadoutSettings.LoadoutPick3'

    Begin Object Class=moComboBox Name=LoadoutPick4
         bReadOnly=True
         ComponentJustification=TXTA_Left
         CaptionWidth=0.700000
         Caption="Loadout Item"
         OnCreateComponent=LoadoutPick4.InternalOnCreateComponent
         IniOption="@Internal"
         IniDefault="None"
         Hint="Changes the item you will get when you purchase this loadout."
         WinTop=0.150261
         WinLeft=0.301757
         WinWidth=0.374219
         TabOrder=2
    End Object
    co_LoadoutPick[4]=moComboBox'Taskforces.TFTab_LoadoutSettings.LoadoutPick4'

    Begin Object Class=moComboBox Name=LoadoutPick5
         bReadOnly=True
         ComponentJustification=TXTA_Left
         CaptionWidth=0.700000
         Caption="Loadout Item"
         OnCreateComponent=LoadoutPick5.InternalOnCreateComponent
         IniOption="@Internal"
         IniDefault="None"
         Hint="Changes the item you will get when you purchase this loadout."
         WinTop=0.150261
         WinLeft=0.301757
         WinWidth=0.374219
         TabOrder=2
    End Object
    co_LoadoutPick[5]=moComboBox'Taskforces.TFTab_LoadoutSettings.LoadoutPick5'

    Begin Object Class=moComboBox Name=LoadoutPick6
         bReadOnly=True
         ComponentJustification=TXTA_Left
         CaptionWidth=0.700000
         Caption="Loadout Item"
         OnCreateComponent=LoadoutPick6.InternalOnCreateComponent
         IniOption="@Internal"
         IniDefault="None"
         Hint="Changes the item you will get when you purchase this loadout."
         WinTop=0.150261
         WinLeft=0.301757
         WinWidth=0.374219
         TabOrder=2
    End Object
    co_LoadoutPick[6]=moComboBox'Taskforces.TFTab_LoadoutSettings.LoadoutPick6'

    Begin Object Class=moComboBox Name=LoadoutPick7
         bReadOnly=True
         ComponentJustification=TXTA_Left
         CaptionWidth=0.700000
         Caption="Loadout Item"
         OnCreateComponent=LoadoutPick7.InternalOnCreateComponent
         IniOption="@Internal"
         IniDefault="None"
         Hint="Changes the item you will get when you purchase this loadout."
         WinTop=0.150261
         WinLeft=0.301757
         WinWidth=0.374219
         TabOrder=2
    End Object
    co_LoadoutPick[7]=moComboBox'Taskforces.TFTab_LoadoutSettings.LoadoutPick7'

    Begin Object Class=moComboBox Name=LoadoutPick8
         bReadOnly=True
         ComponentJustification=TXTA_Left
         CaptionWidth=0.700000
         Caption="Loadout Item"
         OnCreateComponent=LoadoutPick8.InternalOnCreateComponent
         IniOption="@Internal"
         IniDefault="None"
         Hint="Changes the item you will get when you purchase this loadout."
         WinTop=0.150261
         WinLeft=0.301757
         WinWidth=0.374219
         TabOrder=2
    End Object
    co_LoadoutPick[8]=moComboBox'Taskforces.TFTab_LoadoutSettings.LoadoutPick8'

    Begin Object Class=GUIButton Name=SaveButton
         Caption="Save This Loadout"
         Hint="Save the current loadout."
         WinTop=0.901559
         WinLeft=0.043685
         WinWidth=0.130720
         WinHeight=0.050000
         TabOrder=3
         OnClick=SaveOnClick
     End Object
     b_savebutton=GUIButton'Taskforces.TFTab_LoadoutSettings.SaveButton'

     PanelCaption="Loadouts"
     WinTop=0.150000
     WinHeight=0.720000
}
